import dayjs from "dayjs"
import { uploadFile, message } from "@/common/utils.js"
import * as apis from "@/apis/modules/auth.js"

const auth = {
	namespaced: true,
	state: {
		files: {
		  personFront: '',
		  personCard: '',
		  idCardFront: '',
		  idCardBack: '',
		  bankCard: ''
		},
		authInfo: {
			agentName: '',
			agentIdNo: '',
			gender: '2',
			idNoStart: Number(new Date()),
			idNoEnd: Number(new Date()),
			agentBankcardNo: '',
			agentBankName: ''
		}
	},
	mutations: {
		save(state, {field, payload}) {
		  if (Object.prototype.toString.call(payload) === '[object Object]') {
			state[field] = { ...state[field], ...payload }
		  } else {
			state[field] = payload
		  }
		}
	},
	actions: {
		filesChange({ commit }, payload) {
			
			if (payload.file) {
				uploadFile(payload.file, (file) => {
					commit('save', { field: 'files', payload: {[payload.name]: file} })
				})
			} else {
				commit('save', { field: 'files', payload: {[payload.name]: null} })
			}
		},
		
		submit({commit, state}) {
			const {authInfo, files} = state
			const {agentName, agentIdNo, idNoStart, idNoEnd, agentBankcardNo, agentBankName} = authInfo
		
			const getUploadValue = (value) => value.includes('*') ? undefined : value
			
			const agentCertification = {
				agentBankcard: {
					agentIdNo: getUploadValue(agentIdNo),
					agentName: getUploadValue(agentName),
					agentBankName: getUploadValue(agentBankName),
					agentBankcardNo: getUploadValue(agentBankcardNo),
					idNoStart: getUploadValue(idNoStart),
					idNoEnd: getUploadValue(idNoEnd)
				},
				files: [
					{
						fileType: '15',
						fileUrl: files.personFront.fileUrl
					},
					{
						fileType: '16',
						fileUrl: files.personCard.fileUrl
					},
					{
						fileType: '02',
						fileUrl: files.idCardFront.fileUrl
					},
					{
						fileType: '03',
						fileUrl: files.idCardBack.fileUrl
					},
					{
						fileType: '05',
						fileUrl: files.bankCard.fileUrl
					}
				]
			}
			
			return apis.submit(agentCertification)
				.then(({data}) => {
					commit('home/save', {field: 'agentInfo', payload: {agentStatus: '2'}}, {root: true})
					return true
				})
		},
		
		identifyBankcard({commit}, cardNo) {
			apis.identifyBankcard(cardNo)
				.then(({data}) => {
					commit('save', {field: 'authInfo', payload: {agentBankName: data.bnkBnm}})
				})
		},
		
		getAuthInfo({commit}, agentInfo) {
			const { agentName, gender, agentIdNo, agentBankcardNo, agentBankName, idNoStart, idNoEnd, idNoAddress } = agentInfo
			commit('save', {field: 'authInfo', payload: {agentName, gender, agentIdNo, agentBankcardNo, agentBankName, idNoStart, idNoEnd, idNoAddress}})
			
			apis.getAuthFiles()
				.then(({data}) => {
					data.forEach((pic) => {
						pic.url = pic.fileUrl
						pic.thumb = pic.fileUrl
					})

					const [personFront, personCard, idCardFront, idCardBack, bankCard] = data

					const files = {
						personFront,
						personCard,
						idCardFront,
						idCardBack,
						bankCard
					}

					commit('save', {field: 'files', payload: files})
				})
		},
		
		getEidToken({state: {authInfo: {name, idCardNo}}}) {
			return apis.getEidToken()
		}
	}
}

export default auth